<?php

namespace App\Models;


use App\Models\Video\Video;
use App\Services\Base\HelpService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class Content extends BaseModel
{
    protected $table = 'contents';
    protected $dateFormat = 'U';

    const TYPE_SCROLL = 50;
    public static $lib_type = [
        10 => '文章',
        20 => '视频',
        30 => '图片',
        40 => '视频广告',
        self::TYPE_SCROLL => '视频轮播',
    ];


    public static $lib_lang = [
        'en' => '英文',
        'zh_CN' => '中文',
        'zh_TW' => '中文(繁体)',
    ];


    protected $guarded = [];

    public function category()
    {
        return $this->belongsTo(ContentCategory::class, 'category_id', 'sn');
    }

    public function getList($data, $limit = 10)
    {
        //8001banner(1) 8002平台公告(2) 8003服务条款(3)  8004帮助(4)
        $param = ['type', 'lang'];
        $type = $data['type'] ?? 0;
        $result = [];
        $sn = "";
        $field = [];
        $lang = $data['lang'];
        switch ($type) {
            case 1;
                $sn = 8001;
                $field = ['contents.id', 'title', 'image', 'contents.created_at'];
                break;
            case 2;
                $sn = 8002;
                $field = ['contents.id', 'title', 'image', 'contents.created_at'];
                break;
            case 3;
                $sn = 8003;
                $field = ['contents.id', 'title', 'image', 'body', 'contents.created_at'];
                break;
            case 4;
                $sn = 8004;
                $field = ['contents.id', 'title', 'image', 'body', 'contents.created_at'];
                break;
        }
        if ($sn) {
            $result = $this->getModel()->newQuery()->leftJoin('content_category', 'contents.category_id', '=', 'content_category.sn')
                ->where('contents.status', STATUS_ENABLED)
                ->where('contents.lang', $lang)
                ->where('content_category.sn', $sn)
                ->limit($limit)->select($field)->get()->toArray();

            if ($sn == 8001) {
                foreach ($result as $k => &$v) {
                    $v['image'] = g_app_image($v['image']);
                }
            }
        }

        return $result;
    }

    public static function notificationHome()
    {
        $locale = app()->getLocale();
        return Cache::remember("content:notification:home:{$locale}", 3600, function()use($locale){
            $model_category = ContentCategory::oneAsNotification();
            if(!$model_category)
                return false;

            $model = self::query()
                ->whereNull("deleted_at")
                ->where("lang", $locale)
                ->where("category_id", $model_category->sn)
                ->orderByDesc("id")
                ->first();

            return $model ? [
                "title" => $model->title,
                "body" => $model->body,
                "id" => $model->id,
            ]: [
                "title" => __("Countdown to BHE-Carbon Neutral Project Launch"),
                "body" => __("Countdown to BHE-Carbon Neutral Project Launch"),
                "id" => 123,
            ];
        });
    }

    public static function teamDesc()
    {
        $locale = app()->getLocale();
        return Cache::remember("content:team:Desc:{$locale}", 3600, function()use($locale){
            $model_category = ContentCategory::oneAsTeam();
            if(!$model_category)
                return false;

            $model = self::query()
                ->whereNull("deleted_at")
                ->where("lang", $locale)
                ->where("category_id", $model_category->sn)
                ->orderByDesc("id")
                ->first();

            return $model ? $model->body:__("You can update it in the administrator console.");
        });
    }

}
